package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.req.UserListReq;
import com.example.xpvideo.entity.res.AdminListRes;
import com.example.xpvideo.entity.res.PhpLocalAdminRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LsShopAdminMapper;
import com.example.xpvideo.mapper.PhpLocalAdminMapper;
import com.example.xpvideo.service.PhpLocalAdminService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/8/25 17:56
 * @description:
 */
@Service
public class PhpLocalAdminServiceImpl implements PhpLocalAdminService {
    @Autowired
    private PhpLocalAdminMapper phpLocalAdminMapper;
    @Autowired
    private LsShopAdminMapper lsShopAdminMapper;
    @Autowired
    private TimeService timeService;
    @Override
    public ResponseResult<PageResult<List<PhpLocalAdminRes>>> list(UserListReq userListReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String account = userListReq.getAccount();
        String nickname = userListReq.getNickname();


        PageHelper.startPage(page, size);
        List<PhpLocalAdminRes> list =phpLocalAdminMapper.LocalAdminList (Wrappers.<LsShopAdmin>query()
                .eq(!ObjectUtils.isEmpty(account),"account",account)
                .eq("lsa.root",2));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PhpLocalAdminRes>> listPageResult = new PageResult<>(count, pages,
                pageSize, pageNum,
                list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> updateLMStage(UpdateReq updateReq) throws ErrorException {
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsShopAdmin lsShopAdmin = new LsShopAdmin();
        lsShopAdmin.setDisable(stage);
        lsShopAdmin.setUpdateTime(String.valueOf(timeService.getTime()));
        int res = lsShopAdminMapper.update(lsShopAdmin, Wrappers.<LsShopAdmin>update().eq("id",id));

        if(res > 0){
            return ResponseResult.responseResult(0,"冻结/解冻商户成功！");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

}
